using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

using AngleSharp.Dom;
using AngleSharp.Html.Dom;
using AngleSharp.Html.Dom.Events;
using AngleSharp.Html.Parser;
using System.Text;

namespace CSS_Test
{
    public class Program
    {
        public static void Main(string[] args)
        {

            var dir = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot");

            //var parser = new AngleSharp.Html.Parser.HtmlParser();

            //var doc = parser.ParseDocument(File.ReadAllText(Path.Combine(dir, "index.html")));

            var dirs = Directory.GetDirectories(dir);

            var table = "".ToHtmlDocument();

            var sbTbl = new StringBuilder();
            sbTbl.Append("<table>");
            foreach (var dr in dirs)
            {
                sbTbl.Append("<tr>");

                var paths = Directory.GetFiles(dr, "*.html");
                var drName = new DirectoryInfo(dr).Name;
                sbTbl.Append("<td rowspan='" + (paths.Length + 1) + "'>" + drName + "</td>");

                sbTbl.Append("</tr>");


                foreach (var path in paths)
                {
                    sbTbl.Append("<tr>");

                    var url = path.Substring(path.IndexOf("wwwroot")).Replace("\\", "/").Replace("wwwroot/", "");
                    var fileName = Path.GetFileName(path);
                    sbTbl.Append($"<td><a href='{url}' target='_blank'>{fileName}</a></td>");

                    sbTbl.Append("</tr>");
                }
               
            }
            sbTbl.Append("</table>");

            var res = sbTbl.ToString();

           CreateHostBuilder(args).Build().Run();
        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                });
    }
}
